﻿using GameCreator.Characters;
using Gp.Scripts.Character;
using Gp.Scripts.Core;
using QFramework;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Helpers
{
    public static class ObjectHelper
    {
        public static Camera MainCamera =>
            SceneRoot.Get<PlayerRoot>().MainCamera;

        public static PlayerBaseCharacter PlayerCharacter
            => SceneRoot.Get<PlayerRoot>().PlayerCharacter;


        public static bool IsPlayer(GameObject @object)
        {
            if (!PlayerCharacter) return false;
            return @object.GetInstanceID() == PlayerCharacter.gameObject.GetInstanceID();
        }


        public static void LoadModel(this EntityBehaviour self)
        {
            if (self == null) return;

            var config = self.GetConfig();
            if (Application.isPlaying)
            {
                var resLoader = self.Entity.ResLoader;
                var model     = resLoader.LoadSync<GameObject>(ConstBundle.UNIT_MODEL, config.ModelName);
                self.GetComponent<CharacterAnimator>().ChangeModel(model);
            }
            # if UNITY_EDITOR
            else
            {
                var model = ResHelper.LoadAssetInEditor<GameObject>(ConstBundle.UNIT_MODEL, config.ModelName);
                LoadCharacterModelInEditMode(self.GetComponent<CharacterAnimator>(), model);
            }
            #endif
        }

        public static void LoadModel(this BaseCharacter self)
        {
            LoadModel(self.GetComponent<EntityBehaviour>());
        }

        #if UNITY_EDITOR
        public const string CHARACTER_ANIMATOR_CONTROLLER =
            "Assets/AAA/Art/Animation/Animator/Character Animator.controller";
        #endif

        public static void LoadCharacterModelInEditMode(CharacterAnimator characterAnimator, GameObject prefab)
        {
            #if UNITY_EDITOR
            if (prefab == null) return;
            if (prefab.GetComponentInChildren<Animator>() == null) return;
            if (PrefabUtility.IsPartOfNonAssetPrefabInstance(characterAnimator.gameObject))
            {
                EditorWindow.focusedWindow.ShowNotification(new GUIContent("使用预制件!"));
                return;
            }

            GameObject instance = prefab.InstantiateWithParent(characterAnimator.transform)
                .LocalPositionIdentity()
                .LocalRotationIdentity()
                .Name(prefab.name);

            Animator instanceAnimator = instance.GetComponentInChildren<Animator>();
            RuntimeAnimatorController rc =
                AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(CHARACTER_ANIMATOR_CONTROLLER);
            instanceAnimator.runtimeAnimatorController = rc;

            Animator previous = characterAnimator.animator;
            if (previous)
                Object.DestroyImmediate(previous.gameObject);
            
            characterAnimator.animator = instanceAnimator;
            EditorUtility.SetDirty(characterAnimator);
            #endif
        }
    }
}